/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "napi_init.h"
#include "napi_file_share.h"
#include "file_share_log.h"

namespace OHOS {
namespace FileShare {
namespace {
const std::string SHARE_MANAGER_NAPI_CLASS_NAME = "ShareManager";
}
/*
 * Module export function
 */
static napi_value NapiInit(napi_env env, napi_value exports) {
  /*
   * Propertise define
   */
  napi_value dmClass = nullptr;
  napi_property_descriptor desc[] = {
      DECLARE_NAPI_FUNCTION("enableShare", NapiFileShare::EnableShare),
      DECLARE_NAPI_FUNCTION("disableShare", NapiFileShare::DisableShare),
      DECLARE_NAPI_FUNCTION("getShareStatus", NapiFileShare::GetShareStatus),
      DECLARE_NAPI_FUNCTION("startScan", NapiFileShare::StartScan),
      DECLARE_NAPI_FUNCTION("stopScan", NapiFileShare::StopScan),
      DECLARE_NAPI_FUNCTION("connect", NapiFileShare::Connect),
      DECLARE_NAPI_FUNCTION("disconnect", NapiFileShare::Disconnect),
      DECLARE_NAPI_FUNCTION("confirmConnect", NapiFileShare::ConfirmConnect),
      DECLARE_NAPI_FUNCTION("refuseConnect", NapiFileShare::RefuseConnect),
      DECLARE_NAPI_FUNCTION("on", NapiFileShare::JsOn),
      DECLARE_NAPI_FUNCTION("off", NapiFileShare::JsOff),
      DECLARE_NAPI_FUNCTION("sendFile", NapiFileShare::SendFile),
      DECLARE_NAPI_FUNCTION("cancelSendFile", NapiFileShare::CancelSendFile),
      DECLARE_NAPI_FUNCTION("abortReceiveFile", NapiFileShare::AbortReceiveFile),
      DECLARE_NAPI_FUNCTION("getShareStoragePath", NapiFileShare::GetShareStoragePath),
      DECLARE_NAPI_FUNCTION("setShareStoragePath", NapiFileShare::SetShareStoragePath),
      DECLARE_NAPI_FUNCTION("getShareOpenTime", NapiFileShare::GetShareOpenTime),
      DECLARE_NAPI_FUNCTION("setShareOpenTime", NapiFileShare::SetShareOpenTime),
      DECLARE_NAPI_FUNCTION("getAvailableDeviceList", NapiFileShare::GetAvailableDeviceList)
  };

  napi_property_descriptor static_prop[] = {
      DECLARE_NAPI_STATIC_FUNCTION("createShareManager", NapiFileShare::CreateShareManager),
      DECLARE_NAPI_STATIC_FUNCTION("releaseShareManager", NapiFileShare::ReleaseShareManager),
  };

  LOGI("NapiFileShare constructor is called.");
  NAPI_CALL(env, napi_define_class(env, SHARE_MANAGER_NAPI_CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, NapiFileShare::Constructor,
                                    nullptr, sizeof(desc) / sizeof(desc[0]), desc, &dmClass));
  NAPI_CALL(env, napi_create_reference(env, dmClass, 1, &NapiFileShare::sConstructor));
  NAPI_CALL(env, napi_set_named_property(env, exports, SHARE_MANAGER_NAPI_CLASS_NAME.c_str(), dmClass));
  NAPI_CALL(env, napi_define_properties(env, exports, sizeof(static_prop) / sizeof(static_prop[0]), static_prop));
  LOGI("NapiFileShare constructor successfully.");
  return exports;
}

/*
 * Module define
 */
static napi_module _module = {.nm_version = 1,
                              .nm_flags = 0,
                              .nm_filename = nullptr,
                              .nm_register_func = NapiInit,
                              .nm_modname = "oneconnect.fileshare",
                              .nm_priv = ((void *)0),
                              .reserved = {0}};

/*
 * Module register function
 */
extern "C" __attribute__((constructor)) void RegisterModule(void) {
  napi_module_register(&_module);
}
} // namespace FileShare
} // namespace OHOS
